/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.sql.SQLException;
import java.util.List;
import sys.bean.CidadeBean;
import sys.bean.EmpresaBean;
import sys.bean.EstadoBean;
import sys.dao.CidadeDao;
import sys.dao.EmpresaDao;
import sys.dao.EstadoDao;
import sys.utils.LetterUpper;
import sys.utils.Mensagens;
import sys.utils.Utils;

/**
 *
 * @author Administrador
 */
public class CCadastroEmpresa extends javax.swing.JDialog {
    
    List<CidadeBean> listaCidades;
    List<EstadoBean> listaEstados;
    
    CidadeDao daoCidade = new CidadeDao();
    EstadoDao daoEstado = new EstadoDao();
    EmpresaDao daoEmpresa = new EmpresaDao();
    EmpresaBean empresa;
    
    private boolean inserir;


    /**
     * Creates new form CCadastroEmpresa
     *
     * @param parent
     * @param modal
     */
    public CCadastroEmpresa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        Utils.escClose(this);
        atualizaEstados();
        carregaEmpresa();
    }
      
    public void carregaEmpresa() {
        try {
            empresa = daoEmpresa.consultaEmpresa();
            
            txCodigo.setText(empresa.getIdEmpresa() + "");
            txCnpj.setText(empresa.getCnpj());
            txIE.setText(empresa.getIe());
            txRazaoSocial.setText(empresa.getRazaoSocial());
            txNomeFantasia.setText(empresa.getNomeFantasia());
            txEndereco.setText(empresa.getEndereco());
            txNEndereco.setText(empresa.getnEndereco());
            txBairro.setText(empresa.getBairro());
            
            cbEstado.getModel().setSelectedItem(empresa.getCidade().getEstado().getNome() + "-" + empresa.getCidade().getEstado().getUf());
            cbCidade.getModel().setSelectedItem(empresa.getCidade().getNome() + "-" + empresa.getCidade().getEstado().getUf());
            
            txCep.setText(empresa.getCep());
            txFone.setText(empresa.getFone());
            
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao consultar dados da empresa\n" + ex.getMessage());
        }
        
    }
    
    private EmpresaBean retornaObjeto() {
        
        EmpresaBean emp = new EmpresaBean();
        CidadeBean cidade = new CidadeBean();
        if (!isInserir()) {
            emp.setIdEmpresa(Integer.parseInt(txCodigo.getText()));
        }
        cidade.setIdCidage(listaCidades.get(cbCidade.getSelectedIndex() - 1).getIdCidage());
        emp.setCidade(cidade);
        emp.setRazaoSocial(txRazaoSocial.getText());
        emp.setNomeFantasia(txNomeFantasia.getText());
        emp.setEndereco(txEndereco.getText());
        emp.setnEndereco(txNEndereco.getText());
        emp.setBairro(txBairro.getText());
        emp.setCep(txCep.getText());
        emp.setFone(txFone.getText());
        emp.setCnpj(txCnpj.getText());
        emp.setIe(txIE.getText());
        
        return emp;
    }
    
    private void atualizaEstados() {
        cbEstado.removeAllItems();
        try {
            listaEstados = daoEstado.listaEstados();
            cbEstado.addItem("<< SELECIONE ESTADO >>");
            for (EstadoBean lEstados : listaEstados) {
                cbEstado.addItem(lEstados.getNome() + "-" + lEstados.getUf());
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao atualizar estados\n" + ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    private void atualizaCidades() {
        cbCidade.removeAllItems();
        try {
            listaCidades = daoCidade.listaCidades(listaEstados.get(cbEstado.getSelectedIndex() - 1).getIdEstado());
            cbCidade.addItem("<< SELECIONE CIDADE >>");
            for (CidadeBean lCidades : listaCidades) {
                cbCidade.addItem(lCidades.getNome() + "-" + lCidades.getUf());
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao atualizar cidades\n" + ex.getMessage());
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txRazaoSocial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txNomeFantasia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txIE = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txEndereco = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txNEndereco = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txBairro = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbEstado = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        cbCidade = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txCnpj = new javax.swing.JFormattedTextField();
        txCep = new javax.swing.JFormattedTextField();
        txFone = new javax.swing.JFormattedTextField();
        jPanel7 = new javax.swing.JPanel();
        btnSalvar4 = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MANUTENÇÃO DE CADASTRO");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DADOS DA EMPRESA:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel1.setText("CODIGO:");

        txCodigo.setEditable(false);
        txCodigo.setBackground(new java.awt.Color(245, 245, 245));
        txCodigo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txCodigo.setForeground(new java.awt.Color(51, 51, 255));
        txCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("RAZÃO SOCIAL:");

        txRazaoSocial.setDocument(new LetterUpper());
        txRazaoSocial.setBackground(new java.awt.Color(245, 245, 245));
        txRazaoSocial.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txRazaoSocial.setForeground(new java.awt.Color(51, 51, 255));

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel3.setText("NOME FANTASIA:");

        txNomeFantasia.setDocument(new LetterUpper());
        txNomeFantasia.setBackground(new java.awt.Color(245, 245, 245));
        txNomeFantasia.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txNomeFantasia.setForeground(new java.awt.Color(51, 51, 255));

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel4.setText("CNPJ:");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel5.setText("INSCRIÇÃO ESTADUAL:");

        txIE.setBackground(new java.awt.Color(245, 245, 245));
        txIE.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txIE.setForeground(new java.awt.Color(51, 51, 255));
        txIE.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel6.setText("ENDEREÇO:");

        txEndereco.setDocument(new LetterUpper());
        txEndereco.setBackground(new java.awt.Color(245, 245, 245));
        txEndereco.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txEndereco.setForeground(new java.awt.Color(51, 51, 255));

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel7.setText("NÚMERO:");

        txNEndereco.setDocument(new LetterUpper());
        txNEndereco.setBackground(new java.awt.Color(245, 245, 245));
        txNEndereco.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txNEndereco.setForeground(new java.awt.Color(51, 51, 255));
        txNEndereco.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel8.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel8.setText("BAIRRO:");

        txBairro.setDocument(new LetterUpper());
        txBairro.setBackground(new java.awt.Color(245, 245, 245));
        txBairro.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txBairro.setForeground(new java.awt.Color(51, 51, 255));

        jLabel9.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel9.setText("ESTADO:");

        cbEstado.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        cbEstado.setForeground(new java.awt.Color(51, 51, 255));
        cbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< SELECIONE ESTADO >>" }));
        cbEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbEstadoActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel10.setText("CIDADE:");

        cbCidade.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        cbCidade.setForeground(new java.awt.Color(51, 51, 255));
        cbCidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< SELECIONE CIDADE >>" }));

        jLabel11.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel11.setText("CEP:");

        jLabel12.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel12.setText("TELEFONE:");

        txCnpj.setBackground(new java.awt.Color(245, 245, 245));
        txCnpj.setForeground(new java.awt.Color(51, 51, 255));
        try {
            txCnpj.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txCnpj.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txCnpj.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        txCep.setBackground(new java.awt.Color(245, 245, 245));
        txCep.setForeground(new java.awt.Color(51, 51, 255));
        try {
            txCep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txCep.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txCep.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        txFone.setBackground(new java.awt.Color(245, 245, 245));
        txFone.setForeground(new java.awt.Color(51, 51, 255));
        try {
            txFone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(0##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txFone.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txFone.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(txCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(txCnpj))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel5)
                                .addComponent(txIE, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(txRazaoSocial, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(txEndereco))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(txNEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGap(66, 66, 66))
                                .addComponent(txBairro)))
                        .addComponent(txNomeFantasia, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cbEstado, 0, 203, Short.MAX_VALUE)
                                    .addComponent(jLabel11)
                                    .addComponent(txCep))
                                .addComponent(jLabel9))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cbCidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txFone)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel12)
                                        .addComponent(jLabel10))
                                    .addGap(0, 0, Short.MAX_VALUE))))))
                .addGap(35, 35, 35))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txCnpj)
                            .addComponent(txCodigo)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(28, 28, 28))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txIE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txRazaoSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txNomeFantasia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txNEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txBairro))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(cbCidade, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txFone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel12))
                        .addGap(28, 28, 28)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel7.setBackground(new java.awt.Color(245, 245, 245));
        jPanel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        btnSalvar4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSalvar4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/salvar.png"))); // NOI18N
        btnSalvar4.setText("SALVAR");
        btnSalvar4.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSalvar4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvar4ActionPerformed(evt);
            }
        });
        jPanel7.add(btnSalvar4);

        btnSair.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/sair.png"))); // NOI18N
        btnSair.setText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel7.add(btnSair);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvar4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvar4ActionPerformed
        try {
            if (isInserir()) {
                daoEmpresa.incluir(retornaObjeto());
                this.dispose();
            } else {
                daoEmpresa.alterar(retornaObjeto());
                this.dispose();
            }
        } catch (SQLException ex) {
            Mensagens.msgErro("erro ao cadastrar empresa\n" + ex.getMessage());
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnSalvar4ActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void cbEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbEstadoActionPerformed
        
        try {
            atualizaCidades();
        } catch (java.lang.NullPointerException | java.lang.ArrayIndexOutOfBoundsException e) {
            
        }
    }//GEN-LAST:event_cbEstadoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar4;
    private javax.swing.JComboBox cbCidade;
    private javax.swing.JComboBox cbEstado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTextField txBairro;
    private javax.swing.JFormattedTextField txCep;
    private javax.swing.JFormattedTextField txCnpj;
    private javax.swing.JTextField txCodigo;
    private javax.swing.JTextField txEndereco;
    private javax.swing.JFormattedTextField txFone;
    private javax.swing.JTextField txIE;
    private javax.swing.JTextField txNEndereco;
    private javax.swing.JTextField txNomeFantasia;
    private javax.swing.JTextField txRazaoSocial;
    // End of variables declaration//GEN-END:variables

    public boolean isInserir() {
        return inserir;
    }
    
    public void setInserir(boolean inserir) {
        this.inserir = inserir;
    }
}
